package com.chongyuan.producer.server;

import org.apache.commons.lang3.StringUtils;
import org.quickserver.net.server.ClientHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chongyuan.producer.common.DeviceInfo;
import com.chongyuan.producer.common.RedisConstant;
import com.chongyuan.producer.mq.OrderProducerService;
import com.chongyuan.producer.util.GetUTCTimeUtil;

@Service
public class GpsMsgServer {

	final static String SUFFIX_SPLIT = ",";
	final static String SUFFIX_LEFT = "[";
	final static String SUFFIX_RIGHT = "]";
	final static String SUFFIX_EMPTY = "";
	final static String SUFFIX_HEARTBEAT = "LK"; // 心跳包标识
	final static String SUFFIX_GPS_MSG = "UD"; // 入库gps信息标识
	final static String SUFFIX_INIT_MSG = "INIT"; // 初始化消息

	@Autowired
	private OrderProducerService orderProducerService;

	public String dandleMsg(ClientHandler handler, String command) {
		if (StringUtils.isBlank(command) || !command.startsWith(SUFFIX_LEFT) || !command.endsWith(SUFFIX_RIGHT)) {
			return null;
		}
		String gpsMsg = command.replace(SUFFIX_LEFT, SUFFIX_EMPTY).replace(SUFFIX_RIGHT, SUFFIX_EMPTY);
		String[] msgArray = gpsMsg.split(SUFFIX_SPLIT);
		if (msgArray.length < 2) {
			return null;
		}
		String[] headMsgArray = msgArray[0].split("\\*");
		String firm = headMsgArray[0];// 厂商标识
		String deviceId = headMsgArray[1];// 设备ID
		String serialNo = headMsgArray[2];// 流水号
		DeviceInfo deviceInfo = (DeviceInfo) RedisConstant.socektMap.get(RedisConstant.DEVICE_SOCKET_CLIENT + deviceId);
		if (deviceInfo == null || !deviceInfo.getIsRuning()) {
			deviceInfo = new DeviceInfo();
			deviceInfo.setHandler(handler);
			deviceInfo.setIsRuning(false);
			deviceInfo.setDeviceId(deviceId);
			RedisConstant.socektMap.put(RedisConstant.DEVICE_SOCKET_CLIENT + deviceId, deviceInfo);
		}

		if (msgArray[0].endsWith(SUFFIX_HEARTBEAT)) {
			String date = GetUTCTimeUtil.getUTCTimeStr();
			date = date.replace(" ", ",");
			StringBuffer returnBuf = new StringBuffer(SUFFIX_LEFT);
			returnBuf.append(firm);
			returnBuf.append("*");
			returnBuf.append(deviceId);
			returnBuf.append("*");
			returnBuf.append(serialNo);
			returnBuf.append("*0016*LK,");
			returnBuf.append(date);
			returnBuf.append(SUFFIX_RIGHT);
			return returnBuf.toString();
		} else if (msgArray[0].endsWith(SUFFIX_GPS_MSG)) {
			orderProducerService.sendMessage(gpsMsg);
			StringBuffer returnBuf = new StringBuffer(SUFFIX_LEFT);
			returnBuf.append(firm);
			returnBuf.append("*");
			returnBuf.append(deviceId);
			returnBuf.append("*");
			returnBuf.append(serialNo);
			returnBuf.append("*0002*UD]");
			return returnBuf.toString();
		} else if (msgArray[0].endsWith(SUFFIX_INIT_MSG)) {
			StringBuffer returnBuf = new StringBuffer(SUFFIX_LEFT);
			returnBuf.append(firm);
			returnBuf.append("*");
			returnBuf.append(deviceId);
			returnBuf.append("*");
			returnBuf.append(serialNo);
			returnBuf.append("*");
			returnBuf.append("0006*INIT,1]");
			// String uploadReply = "[" + firm + "*" + deviceId + "*" + serialNo
			// + "*" + "0009*UPLOAD" + ",30]";
			// try {
			// handler.sendClientMsg(uploadReply);
			// } catch (IOException e) {
			// }
			return returnBuf.toString();
		}
		return null;
	}

}
